<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>审核</el-breadcrumb-item>
        <el-breadcrumb-item>身份审核</el-breadcrumb-item>
        <el-breadcrumb-item>作者</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @selection-change="selectionChange"
      >
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            plain
            @click.stop="editSelect(true)"
            >通过</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            plain
            @click.stop="editSelect(false)"
            >拒绝</el-button
          >
          <br />
          <!-- <el-tag> 已通过n条 未通过n条 待审核n条</el-tag> -->
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <!-- {{sope}} -->
          <!-- <el-button icon="el-icon-edit" type="text" size="small" @click.stop="reply(scope)">{{scope.row.reply_id ? "编辑回复":"回复"}}</el-button> -->
          <el-button
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="adopt(scope)"
            >通过</el-button
          >
          <el-button
            icon="el-icon-delete"
            type="text"
            size="small"
            @click.stop="refuse(scope)"
            >拒绝</el-button
          >
        </template>

        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_account_name }}</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { catAuthor as baseURL } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      // 默认查询条件
      default: {
        orderByList: "create_time&DESC",
        type: 0,
      },
      // 勾选选中的内容
      checks: [],
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        selection: true,
        border: true,
        column: [
          {
            label: "日期",
            prop: "create_time",
            type: "daterange",
            search: true,
          },
          {
            label: "用户id",
            prop: "user_id",
            type: "input",
            search: true,
          },
          {
            label: "用户昵称",
            prop: "user_name",
            type: "input",
          },
          {
            label: "姓名",
            prop: "real_name",
            type: "input",
          },
          {
            label: "性别",
            prop: "sex",
            type: "select",
            search: true,
            dicData: [
              {
                label: "未知",
                value: 0,
              },
              {
                label: "男",
                value: 1,
              },
              {
                label: "女",
                value: 2,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "电话",
            prop: "phone",
            type: "input",
          },
          {
            label: "邮箱",
            prop: "email",
            type: "input",
          },
          {
            label: "QQ",
            prop: "qq",
            type: "input",
          },
          {
            label: "联系地址",
            prop: "address",
            type: "textarea",
          },
          {
            label: "个人简介",
            prop: "personal_des",
            type: "textarea",
          },
          {
            label: "审核状态",
            prop: "state",
            type: "select",
            hide: false,
            search: false,
            dicData: [
              {
                label: "<font style='color:red'>待审核</font>",
                value: 0,
              },
              {
                label: "已通过",
                value: 1,
              },
              {
                label: "未通过",
                value: 2,
              },
            ],
          },
          {
            label: "审核状态",
            prop: "states",
            type: "select",
            hide: true,
            search: true,
            dicData: [
              {
                label: "待审核",
                value: 0,
              },
              {
                label: "已通过",
                value: 1,
              },
              {
                label: "未通过",
                value: 2,
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  },
  methods: {
    //某个不通过
    async refuse(scope) {
      if (scope.row.state == 2) {
        return this.$message.error("已是该状态,不可重复修改");
      }
      
      if(scope.row.state==1){
        return this.$message.error("不可拒绝已通过的申请");
      }
      this.$confirm(`<strong>拒绝</strong> 该用户作者申请, 是否继续?`, '王牌特战小提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(async () => {
        let userInfo = this.$store.state.user.userInfo;
        scope.row.state = 2;
        scope.row.edit_time = new Date();
        scope.row.edit_account_name = userInfo.username;
        let data = await common(`${baseURL.update}`, scope.row, null, "POST");
        this.getList();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    //某个通过
    async adopt(scope) {
      if (scope.row.state == 1) {
        return this.$message.error("已是该状态,不可重复修改");
      }
      if(scope.row.state==2){
        return this.$message.error("不可通过已拒绝的申请");
      }
      this.$confirm(`<strong>通过</strong> 该用户作者申请, 是否继续?`, '王牌特战小提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(async () => {
        let userInfo = this.$store.state.user.userInfo;
        scope.row.state = 1;
        scope.row.edit_time = new Date();
        scope.row.edit_account_name = userInfo.username;
        let data = await common(`${baseURL.update}`, scope.row, null, "POST");
        this.getList();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    /**
     * 全选的通过或拒绝
     * type : 2表示通过,4表示拒绝
     */
    async editSelect(type) {
      if (this.checks == undefined || this.checks.length == 0) {
        return this.$message.error("请选择要修改的内容");
      }
      let arr = this.checks.filter((item) => item.state == (type ? 1 : 2));
      if (arr.length > 0) {
        return this.$message.error("选择内容存在该状态,不可修改");
      }
      
      let arrtwo = this.checks.filter(item=> item.state==2);
      if(arrtwo.length>0){
        return this.$message.error("不可通过已拒绝的申请");
      }
      this.$confirm(`<strong>${type?"通过":"拒绝"}</strong> 选中的<strong>(${this.checks.length}条)</strong>用户作者申请, 是否继续?`, '王牌特战小提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(async () => {
        let userInfo = this.$store.state.user.userInfo;
        this.checks.forEach((item) => {
          item.state = type ? 1 : 2;
          item.edit_time = new Date();
          item.edit_account_name = userInfo.username;
        });
        let data = await common(
          `${baseURL.updateAll}`,
          this.checks,
          null,
          "POST"
        );
        this.getList();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    /**勾选所有 */
    selectionChange(row) {
      this.checks = row;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据 
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      
      params.create_time = `${
        params.create_time ? "!<>!" + params.create_time : ""
      }`;
      params.state = params.states;
      delete params.states;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>